package thread1;


import java.util.Random;
import java.util.concurrent.*;

/**
 * @Auther: 杨通通
 * @Date: 2025/1/20 23:42
 * @Description:
 */
public class ThreadList1 {

    public static void main(String[] args) throws InterruptedException {
        CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();

        ExecutorService service = Executors.newFixedThreadPool(10);


        CountDownLatch countDownLatch = new CountDownLatch(20);

        for (int i = 0; i < 20; i++) {
            Integer num = i;
            service.execute(() -> {
                Random random = new Random();
                try {
                    Thread.sleep((random.nextInt(10)+1)*1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                list.add(num);
                countDownLatch.countDown();
            });
        }

    countDownLatch.await();
       list.forEach(System.out::println);
    }


}